"""
This module contains the FileBrowser class used to view
the Landis-II file hierarchy.
"""
"""
   Copyright 2011 Gabriel Elkind and Brian Gapinski

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""
import wx
import re
import logging

logger = logging.getLogger('filebrowser')
class FileBrowser(wx.TreeCtrl):
    """
    Displays the file hierarchy of a Landis-II project.

    @ivar _lime: The parent container
    @type _lime: Lime

    @ivar _model: The model that this tree displays
    @type _model: Model
    """
    def __init__(self, lime, *args, **kwargs):
        """
        The FileBrowser extends the built in wx.TreeCtrl class to provide 
        additional functionality for trees displaying information of file 
        hierarchies.
        
        @param lime: The parent of the FileBrowser module
        @type lime: Lime class
        """
        logger.debug('filebrowser created')
        wx.TreeCtrl.__init__(self, parent=lime, size=(200,600),\
            *args, **kwargs)
        self._lime = lime    
        self._model = None
        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self._OnSelect, self)

    def SetModel(self,model):
        """
        Sets the model for the FileBrowser

        @type model: Model
        @param model: The new model to be set
        """
        self._model = model

    def Notify(self,filename=''):
        """
        Notifies this object that a file has
        changed within the central model and
        makes any relevant changes
        
        @param filename: The file which has been modified or
                         None to indicate that a new project
                         has been opened
        @type filename:  string
        """
	if filename == '':
            top = self._model.GetTree()
            root = self.AddRoot(top.GetName())
        
	    self._DisplayTree(root, top)

    def _DisplayTree(self,parent,tree):
        """
        Creates a visual representation of a tree
        
        @param parent: The parent tree
        @type parent: tree
        
        @param tree: The child tree
        @type tree: tree
        """
        for child in tree.GetChildren():
            title = child.GetName()
            leaf = self.AppendItem(parent,title)
            self._DisplayTree(leaf,child)

    def _OnSelect(self, event):
        """
        Tells lime that an item in the filebrowser has been selected
        """
        filename = self.GetItemText(self.GetSelection())
        self._lime.SelectFile(filename)

